/*
 * @lc app=leetcode.cn id=257 lang=cpp
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<string> binaryTreePaths(TreeNode *root)
    {
        //返回二叉树的所有路径 因为是从上往下 先序遍历即可
        vector<string> res;
        vector<int> path;
        dfs(root, res, path);
        return res;
    }
    void dfs(TreeNode *node, vector<string> &res, vector<int> &path)
    {
        //递归求路径 终止条件是遇到叶子节点停止
        path.push_back(node->val); // jiarujiedian
        //如果当前节点是叶子结点
        if (node->left == NULL && node->right == NULL)
        {
            string tmp = "";
            for (int i = 0; i < path.size() - 1; i++)
            {
                tmp += to_string(path[i]);
                tmp += "->";
            }
            //去除最后一个->
            tmp.pop_
            res.push_back(tmp);
            return;
        }
        if (node->left)
        {
            dfs(node->left, res, path);
            path.pop_back(); //回溯
        }
        if (node->right)
        {
            dfs(node->right, res, path);
        }
        return;
    }
};
// @lc code=end
